<template>
  <el-dialog
    title="新增"
    :close-on-click-modal="false"
    :visible.sync="visible">
    <el-form ref="appForm" :rules="rules" :model="formData" label-width="80px">
      <el-form-item label="应用名称">
        <el-input v-model="formData.appName"></el-input>
      </el-form-item>
      <el-form-item label="数据库简称">
        <el-input v-model="formData.dbName"></el-input>
      </el-form-item>
      <el-form-item label="应用路径">
        <el-input v-model="formData.appPath"></el-input>
      </el-form-item>

      <el-form-item>
        <el-button type="primary" @click="onSubmit">立即创建</el-button>
        <el-button>取消</el-button>
      </el-form-item>
    </el-form>
  </el-dialog>
</template>

<script>
    export default {
      name: 'add-update-info',
      data () {
        return {
          formData: {
            appName: '',
            dbName: '',
            appPath: '',
            id: ''
          },
          visible: false,
          rules: {
            appName: [
              {required: true, message: '请输入应用名称', trigger: 'blur'},
              {min: 3, max: 15, message: '长度在 3 到 15 个字符', trigger: 'blur'}
            ],
            dbName: [
              {required: true, message: '请输入应用数据库简称', trigger: 'blur'},
              {min: 3, max: 10, message: '长度在 3 到 10 个字符', trigger: 'blur'}
            ],
            appPath: [
              {required: true, message: '请输入应用路径', trigger: 'blur'},
              {min: 3, max: 255, message: '长度在 3 到 255 个字符', trigger: 'blur'}
            ]
          }
        }
      },
      activated () {
        this.init()
      },
      methods: {
        init (id) {
          if (id !== undefined) {
            this.dataForm.id = id
          }
          this.visible = true
          this.$nextTick(() => {
            if (this.dataForm.id) {
              this.getAppInfo(this.dataForm.id)
            };
          })
        },
        onSubmit () {
          this.$refs['appForm'].validate((valid) => {
            if (valid) {
              this.$http({
                url: this.$http.adornUrl('/app/saveAppBase'),
                method: 'post',
                params: this.$http.adornParams({
                  'appName': this.formData.appName,
                  'dbName': this.formData.dbName,
                  'appPath': this.formData.appPath
                })
              }).then(({data}) => {
                if (data && data.code === 0) {
                  this.visible = false
                  this.$emit('getAppList')
                }
              })
            } else {
              console.log('error submit!!')
              return false
            }
          })
        },
        getAppInfo (id) {

        }
      }
    }
</script>

<style scoped>

</style>
